# -*- coding: utf-8 -*-
"""
Created on Sat Sep 17 11:18:12 2022

@author: 123
"""


#划分训练集和测试集
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(X,y,stratify=y,random_state=42)

from sklearn.metrics import roc_auc_score,roc_curve,auc

# 调用逻辑 回归模型 
from sklearn.linear_model import LogisticRegression

clf_lr = LogisticRegression() #调用模型
clf_lr.fit(x_train,y_train) #训练模型





#朴素贝叶斯二项分布
import sklearn.naive_bayes as sk_bayes
from sklearn.metrics import classification_report


clf_bn_bin = sk_bayes.BernoulliNB(alpha=1.0,binarize=0.0,fit_prior=True,class_prior=None) #伯努利分布的朴素贝叶斯
clf_bn_bin.fit(x_train,y_train)


#n朴素贝叶斯(二项分布)模型评价: 0.8697628738634753

clf_bn_gau = sk_bayes.GaussianNB()#高斯分布的朴素贝叶斯
clf_bn_gau.fit(x_train,y_train)





#https://blog.csdn.net/ylqDiana/article/details/118764019
#多条ROC曲线绘制函数

 def multi_models_roc(names, sampling_methods, colors, x_test, y_test, save=True, dpin=100):
        """
        将多个机器模型的roc图输出到一张图上
        
        Args:
            names: list, 多个模型的名称
            sampling_methods: list, 多个模型的实例化对象
            save: 选择是否将结果保存（默认为png格式）
            
        Returns:
            返回图片对象plt
        """
        plt.figure(figsize=(20, 20), dpi=dpin)

        for (name, method, colorname) in zip(names, sampling_methods, colors):
            
            y_test_preds = method.predict(x_test)
            y_test_predprob = method.predict_proba(x_test)[:,1]
            fpr, tpr, thresholds = roc_curve(y_test, y_test_predprob, pos_label=1)
            
            plt.plot(fpr, tpr, lw=5, label='{} (AUC={:.3f})'.format(name, auc(fpr, tpr)),color = colorname)
            plt.plot([0, 1], [0, 1], '--', lw=5, color = 'grey')
            plt.axis('square')
            plt.xlim([0, 1])
            plt.ylim([0, 1])
            plt.xlabel('False Positive Rate',fontsize=20)
            plt.ylabel('True Positive Rate',fontsize=20)
            plt.title('ROC Curve',fontsize=25)
            plt.legend(loc='lower right',fontsize=20)

        if save:
            plt.savefig('multi_models_roc.png')
            
        return plt

#调用方法时，需要把模型本身（如clf_xx）、模型名字(如GBDT)和对应颜色（如crimson）按照顺序、以列表形式传入函数作为参数。
"""
names = ['Logistic Regression',
         'Random Forest',
         'XGBoost',
         'AdaBoost',
         'GBDT',
         'LGBM']



sampling_methods = [clf_lr,
                    clf_rf,
                    clf_xgb,
                    clf_adb,
                    clf_gbdt,
                    clf_lgbm
                   ]
# name 'clf_lr' is not defined

colors = ['crimson',
          'orange',
          'gold',
          'mediumseagreen',
          'steelblue', 
          'mediumpurple'  
         ]
"""
names = ['Logistic Regression',
         'sk_bayes BernoulliNB',
         'sk_bayes GaussianNB'
         ]

sampling_methods = [clf_lr,
                    clf_bn_bin,
                    clf_bn_gau
                   ]

colors = ['crimson',
          'orange',
          'gold' 
         ]

#ROC curves
train_roc_graph = multi_models_roc(names, sampling_methods, colors, x_train, y_train, save = True)
train_roc_graph.savefig('ROC_Train_all.png')

